﻿Imports MySql.Data.MySqlClient

Public Class addgroup2_1_2
    Public respone As Object
    Public SQL As MySqlConnection

    Public Sub New(ByRef SQLCon As MySqlConnection)
        InitializeComponent()
        SQL = SQLCon
    End Sub

    '  Private Sub addgroup2_1_2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    'Dim mainForm As main = New main()
    '   mainForm.Show()
    'Me.Close()
    'End Sub

    Private Sub addgroup2_1_2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextCost.Text = "0"
        TextDuration.Text = "00.00"
    End Sub

    Private Sub Insertdata()

        Dim MySqlCommand As New MySqlCommand

        If SQL.State = ConnectionState.Closed Then
            SQL.Open()
        End If
        MsgBox("INSERT INTO Course (course_name,course_start_date,course_cost,course_trainer,course_duration) VALUES ('" & Textcourse.Text & "', '" & DateTimePicker1.Value.Year.ToString() & "-" & DateTimePicker1.Value.Month.ToString() & "-" & DateTimePicker1.Value.Day.ToString() & "', " & TextCost.Text & ", '" & TextTrainer.Text & "'," & TextDuration.Text & ");")

        MySqlCommand.CommandText = "INSERT INTO Course (course_name,course_start_date,course_cost,course_trainer,course_duration) VALUES ('" & Textcourse.Text & "', '" & DateTimePicker1.Value.Year.ToString() & "-" & DateTimePicker1.Value.Month.ToString() & "-" & DateTimePicker1.Value.Day.ToString() & "', " & TextCost.Text & ", '" & TextTrainer.Text & "'," & TextDuration.Text & ");"
        MySqlCommand.CommandType = CommandType.Text
        MySqlCommand.Connection = SQL

        MySqlCommand.ExecuteNonQuery()


        SQL.Close()




        '=========================================

    End Sub

    Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
        Textcourse.Text = ""
        TextCost.Text = ""
        TextTrainer.Text = ""
        TextDuration.Text = ""
    End Sub

    Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click

        If Textcourse.Text = "" Or TextCost.Text = "" Or TextTrainer.Text = "" Or TextDuration.Text = "" Then
            MsgBox("Please complete all field", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Warning Message")
        Else
            respone = MsgBox("Are you sure to save data", MsgBoxStyle.OkCancel + MsgBoxStyle.Information, "Warning Messsage")
            If respone = 1 Then
                Try
                    Insertdata()
                Catch ex As Exception
                    MsgBox("Please check your data again!", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Warning Message")
                    Exit Sub
                End Try

                Dim BeForm As addgroup2_1 = New addgroup2_1(SQL)
                BeForm.Show()

                Me.Close()

            End If
        End If
    End Sub

    Private Sub BackButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackButton.Click
        Dim BeForm As addgroup2_1 = New addgroup2_1(SQL)
        BeForm.Show()

        Me.Close()
    End Sub
End Class